﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using CommonUtility;

namespace BLL
{
    public class bllPermission
    {
        /// <summary>
        /// 修改
        /// </summary>
        public bool isEdit { get; set; }
        /// <summary>
        /// 删除
        /// </summary>
        public bool isRemove { get; set; }
        /// <summary>
        /// 查看
        /// </summary>
        public bool isView { get; set; }

        public string Menu { get; set; }

        public bllPermission()
        {
            this.isEdit = false;
            this.isView = false;
            this.Menu = string.Empty;
        }

        public bllPermission(string _userID)
            : this()
        {
            try
            {
                IList<PermissionView> permList = Permission.GetList("[userID]='" + _userID + "'");
                IList<string> pvList = new List<string>();
                foreach (PermissionView pv in permList)
                {
                    if (!pvList.Contains(pv.FunID))
                        pvList.Add(pv.FunID);
                }
                this.Menu = string.Join(",", pvList.ToArray());
            }
            catch (Exception ex)
            {
                Common.Application_Error_Server("BLL/bllPermission.cs/clsPermission(" + _userID + ")", ex);
            }
        }

        public bllPermission(string _userID, string _FunID)
            : this()
        {
            try
            {
                IList<PermissionView> pvList = Permission.GetList("[userID]='" + _userID + "' AND [funID]='" + _FunID + "'");
                foreach (var pv in pvList)
                {
                    switch (pv.ActID.ToUpper())
                    {
                        case "A01": isEdit = true;//修改
                            break;
                        case "A02": isRemove = true;//删除
                            break;
                        case "A00": isView = true;//查询
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Application_Error_Server("BLL/bllPermission.cs/Permission('" + _userID + "','" + _FunID + "')", ex);
            }
        }


        /// <summary>
        /// 检查有没权限
        /// </summary>
        /// <param name="_userID"></param>
        /// <param name="_FunID"></param>
        /// <param name="_ActID"></param>
        /// <returns></returns>
        public static bool CheckPermission(string _userID, string _FunID, string _ActID)
        {
            bool result = true;
            try
            {
                IList<PermissionView> pvList = Permission.GetList("[userID]='" + _userID + "' AND [funID]='" + _FunID + "' AND [actID]='" + _ActID + "'");
                if (pvList.Count == 0)
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                result = false;
                Common.Application_Error_Server("BLL/bllPermission.cs/CheckPermission('" + _userID + "','" + _FunID + "','"+_ActID+"')", ex);
            }
            return result;
        }
    }
}
